package com.hpeu_01.day0125;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 客户端
 * 模拟登录操作：客户端发送用户名和密码到服务端，服务端验证后返回验证信息给客户端
 */
public class TCPClient {
    public static void main(String[] args) {
        //创建Socket
        try {
            Socket socket = new Socket("localhost",8080);
            // 向服务端发送数据
            OutputStream os = socket.getOutputStream();
            //提交数据
            os.write("hpeu".getBytes());
            os.write("123".getBytes());
            socket.shutdownOutput();

            //接收服务端返回的数据
            InputStream is = socket.getInputStream();
            byte[] b = new byte[1024];
            int len = is.read(b);
            System.out.println(new String(b, 0, len));
            socket.shutdownInput();

            //关资源
            is.close();
            os.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
